package com.net.xpay.backend.scheduler;


import com.net.xpay.common.domain.notify.FlexibleStaffCardOrderNotifyRecord;
import com.net.xpay.common.manager.notify.FlexibleStaffCardOrderNotifyRecordManager;
import com.net.xpay.core.service.safePay.FlexibleStaffCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 2019/7/15 16:28

 * 零工卡开卡回调通知
 */
@Slf4j
@Component
public class FlexibleStaffCardOrderNotifyRecordScheduler {

    @Autowired
    private FlexibleStaffCardOrderNotifyRecordManager flexibleStaffCardOrderNotifyRecordManager;
    
    @Autowired
    private FlexibleStaffCardService flexibleStaffCardService;

    /**
     * 清理过期的数据
     */
    @Scheduled(cron = "0 0 2 * * *")
    public void cleanFlexibleStaffCardOrderNotifyRecord() {
        log.info("cleanFlexibleStaffCardOrderNotifyRecord start");

        //只保留一个月
        LocalDateTime deadline = LocalDateTime.now().minusMonths(1L);
        flexibleStaffCardOrderNotifyRecordManager.deleteFinishedRecord(deadline);

        log.info("ccleanFlexibleStaffCardOrderNotifyRecord end");
    }

    /**
     * 回调通知
     */
    @Scheduled(cron = "*/1 * * * * *")
    public void notifyFlexibleStaffCardNotifyRecord() {
        List<Long> ids = flexibleStaffCardOrderNotifyRecordManager.listWaitNotifyRecordIds(20);

        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Long id : ids) {
            try {
                FlexibleStaffCardOrderNotifyRecord record = flexibleStaffCardOrderNotifyRecordManager.getById(id);
                flexibleStaffCardService.notifyOrderRecord(record);
            } catch (Exception e) {
                log.info("notifyFlexibleStaffCardNotifyRecord failed|id={}", id, e);
            }
        }
    }


}
